package cn.itcast.bos.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.utils.Md5Util;

import cn.itcast.bos.dao.system.RoleDao;
import cn.itcast.bos.dao.system.UserDao;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;


@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private RoleDao roleDao; 
	/**
	  * @Description: 1、添加用户记录 2、用户关联角色
	*/
	public void save(User model, Integer[] roleIds) {
		//密码进行加密
		model.setPassword(Md5Util.encode(model.getPassword()));
		userDao.save(model);
		if(roleIds!=null && roleIds.length>0){
			for (Integer roleId : roleIds) {
				Role role = roleDao.findOne(roleId);
				//用户关联角色     角色放弃维护
				model.getRoles().add(role);//向用户角色关系表T_user_role添加记录
			}
		}
		
	}
	
	
	//用户分页查询
	public Page<User> findAll(Pageable pageable) {
		return userDao.findAll(pageable);
	}

	//TODO NIU RUN SHENG 密码修改
	public void updatePassword(String txtNewPass, Integer id) {
		userDao.update(txtNewPass,id);
		
	}
}
